package com.trainbook.utils.gen;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: Dempsey
 * @Date: 2020/10/19 14:41
 * @Description:
 */

public class CodeGenerator {
    public static void main(String[] args) {
        AutoGenerator mpg = new AutoGenerator();

        //全局配置
        String projectPath = System.getProperty("user.dir");
        GlobalConfig gc = new GlobalConfig()
                .setAuthor("Dempsey")
                .setOutputDir(projectPath+"/src/main/java")
                .setFileOverride(true)
                .setSwagger2(true)
//      gc.setEntityName("%sEntity")
//      gc.setMapperName("%sMapper")
//      gc.setXmlName("%sXmlName")
//      gc.setServiceName("%service")
//      gc.setServiceImplName("ServiceImpl")
//      gc.setControllerName("%sController")
                .setIdType(IdType.ASSIGN_ID)
                .setOpen(false);
        mpg.setGlobalConfig(gc);

        //数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL)
                .setUrl("jdbc:mysql://localhost:3306/trainbook?useUnicode=true&useSSL=false&characterEncoding=utf8")
                .setDriverName("com.mysql.cj.jdbc.Driver")
                .setUsername("root")
                .setPassword("Dempsey33@");
        mpg.setDataSource(dsc);

        //数据库表配置
        StrategyConfig sc = new StrategyConfig();
        sc.setNaming(NamingStrategy.underline_to_camel);
        sc.setEntityLombokModel(true);
        sc.setEntityTableFieldAnnotationEnable(true);
        sc.setVersionFieldName("version");
        sc.setLogicDeleteFieldName("deleted");

        TableFill gmtCreate = new TableFill("gmt_create", FieldFill.INSERT);
        TableFill gmtUpdate = new TableFill("gmt_modified", FieldFill.INSERT_UPDATE);
        List<TableFill> tableFills = new ArrayList<>();
        tableFills.add(gmtCreate);
        tableFills.add(gmtUpdate);
        sc.setTableFillList(tableFills);

//        sc.setSuperControllerClass(Father.class);
//        sc.setInclude();
//        sc.setExclude();

        mpg.setStrategy(sc);

        // 包配置
        PackageConfig pc = new PackageConfig();
//        pc.setModuleName("%s");
        pc.setParent("com.trainbook");
        mpg.setPackageInfo(pc);


        mpg.execute();


    }
}
